package com.jindu.producer.finance.controller;

import com.jindu.kernel.result.JsonResult;
import com.jindu.kernel.utils.AlphaBeanUtils;
import com.jindu.kernel.utils.DateUtils;
import com.jindu.kernel.utils.IdGenerator;
import com.jindu.producer.finance.jpa.MediaJpa;
import com.jindu.producer.finance.model.FFiscalResultEntity;
import com.jindu.producer.finance.model.FMediaEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.List;

/**
 * @author jarvis
 * @date 2018/10/18
 */
@Api(description = "众测报告的图片", tags = "MediaController", basePath = "/media")
@RestController
@RequestMapping(value = "/media",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class MediaController {

    @Autowired
    MediaJpa mediaJpa;

    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "token" , required = false, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "根据报告的ID，查询图片的信息", notes = "返回单评测报告的图片信息")
    @RequestMapping(value = "getByOrgId",method = RequestMethod.GET)
    public JsonResult<List<FMediaEntity>> getByOrgId(@RequestParam String reportId){
        return JsonResult.getSuccessResult(mediaJpa.findByReportId(reportId));
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "token" , required = false, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "保存财税评测结果信息", notes = "保存财税评测结果的信息")
    @RequestMapping(value = "save", method = RequestMethod.POST)
    public JsonResult<FMediaEntity> save(@RequestBody FMediaEntity mediaEntity){

        FMediaEntity fMediaEntity=mediaJpa.findById(mediaEntity.getId());

        if(fMediaEntity == null){
            mediaEntity.setId(IdGenerator.uuid());
            mediaEntity.setCreateTime(new Timestamp(DateUtils.getCurrentTime()));
            mediaEntity.setModifyTime(new Timestamp(DateUtils.getCurrentTime()));
            return JsonResult.getSuccessResult(mediaJpa.saveAndFlush(mediaEntity));
        }else {
            mediaEntity.setModifyTime(new Timestamp(DateUtils.getCurrentTime()));
            BeanUtils.copyProperties(mediaEntity,fMediaEntity, AlphaBeanUtils.getNullPropertyNames(mediaEntity));
            return JsonResult.getSuccessResult(mediaJpa.saveAndFlush(fMediaEntity));
        }
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "token" , required = false, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "删除图片的信息", notes = "根据ID删除数据")
    @RequestMapping(value = "delete",method = RequestMethod.POST)
    public JsonResult<Integer> delete(@RequestBody String id){
        return JsonResult.getSuccessResult(mediaJpa.deleteById(id));
    }



}
